#include "scribblearea.h"

#include <QCloseEvent>
#include <QPainter>
#include <QDebug>

ScribbleArea::ScribbleArea(QWidget *parent) : QWidget(parent)
{
    // Roots the widget to the top left event if resized
    setAttribute(Qt::WA_StaticContents);

    modified = false;
    scribbling = false;
    myPenColor = Qt::blue;
    myPenWidth = 1;

}

void ScribbleArea::clearImage()
{
    image.fill(qRgb(255,255,255));
    modified = true;
    update();
}

void ScribbleArea::print()
{

}

void ScribbleArea::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton && !scribbling)
    {
        lastPaint = event->pos();
        scribbling = true;
        DebugText();
        DebugText(event->pos());
    }
}

void ScribbleArea::mouseMoveEvent(QMouseEvent *event)
{
    if((event->buttons() & Qt::LeftButton) && scribbling)
    {
        drawLineTo(event->pos());
    }

}

void ScribbleArea::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton && scribbling)
    {
        drawLineTo(event->pos());
        scribbling = false;
    }
}

void ScribbleArea::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    QRect dirtyRect = event->rect();

    painter.drawImage(dirtyRect,image,dirtyRect);
}

void ScribbleArea::resizeEvent(QResizeEvent *event)
{
    if(width() > image.width() || height() > image.height())
    {
        int newWidth = qMax(width() + 128, image.width());
        int newHeight = qMax(height() + 128, image.height());
        resizeImage(&image, QSize(newWidth,newHeight));
        update();
    }
    QWidget::resizeEvent(event);
}

void ScribbleArea::drawLineTo(const QPoint &endPoint)
{
    QPainter painter(&image);
    painter.setPen(QPen(myPenColor,myPenWidth,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));

    painter.drawLine(lastPaint, endPoint);

    modified = true;

    int rad = (myPenWidth / 2) + 2;

    update(QRect(lastPaint,endPoint).normalized().adjusted(-rad,-rad,+rad,+rad));

    lastPaint = endPoint;
}

void ScribbleArea::resizeImage(QImage *img, const QSize &newSize)
{
    if(img->size() == newSize)
    {
        return;
    }
    QImage newImage(newSize,QImage::Format_RGB32);
    newImage.fill(qRgb(255,255,255));

    QPainter painter(&newImage);
    painter.drawImage(QPoint(0,0),image);
    image = newImage;
}

bool ScribbleArea::openImage(const QString &fileName)
{
    QImage loadedImage;

    if(!loadedImage.load(fileName))
    {
        return false;
    }

    QSize newSize = loadedImage.size().expandedTo(size());
    resizeImage(&loadedImage,newSize);
    image = loadedImage;
    modified = false;
    // Qwidget 方法
    update();
    return true;
}

bool ScribbleArea::saveImage(const QString &fileName, const char* FileFormat)
{
    QImage visibleImage = image;
    resizeImage(&visibleImage,size());

    if(visibleImage.save(fileName,FileFormat))
    {
        modified = false;
        return true;
    }else{
        return false;
    }
}

void ScribbleArea::setPenColor(const QColor &newColor)
{
    myPenColor = newColor;
}

void ScribbleArea::setPenWidth(int newWidth)
{
    myPenWidth = newWidth;
}

bool ScribbleArea::isModified() const
{
    return false;
}
